package com.wimcorp.magic.abilities;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.wimcorp.magic.abilities.trigger.AbilityTrigger;
import com.wimcorp.magic.behaviour.Behaviour;
import com.wimcorp.magic.behaviour.counter.CardCountCounter;
import com.wimcorp.magic.cards.Creature;
import com.wimcorp.magic.exception.CastException;
import com.wimcorp.magic.input.Location;
import com.wimcorp.magic.input.Target;
import com.wimcorp.magic.input.Type;

/**
 * example {target:'ANY',locations:['BATTLEFIELD'],types :['CREATURE']
 * behaviors:,power :-2 toughness : -2}
 * 
 * target options : ANY,SELF,OPPONENT,THIS
 * 
 * types options : 'CREATURE'
 * 
 * location options : BATTLEFIELD
 * 
 * behaviors options : CARD_COUNT_COUNTER
 * 
 * duration : PERMANENT,END_OF_TURN
 * 
 * counter_target : SELF, OPPONENT, ANY
 * 
 * counter_type : CREATURE,CARD
 * 
 * counter_location : BATTLEFIELD,HAND,GRAVEYARD,EXILE
 * 
 * condition :
 */
@Entity
@Table(name = "addcardcountcounter", schema = "abilities")
public class AddCardCountCounter extends AddCounter {

	@Transient
	private Set<Behaviour> counters = new HashSet<>();
	@Transient
	private Creature creature = null;

	@Enumerated(EnumType.STRING)
	private Target counterTarget;

	@Enumerated(EnumType.STRING)
	private Type counterType;

	@Enumerated(EnumType.STRING)
	private Location counterLocation;

	protected AddCardCountCounter() {
	}

	public AddCardCountCounter(AbilityTrigger trigger) {
		super(trigger);
	}
	
	public AddCardCountCounter(AddCardCountCounter ability) {
		super(ability);
		this.counterLocation=ability.getCounterLocation();
		this.counterTarget=ability.getCounterTarget();
		this.counterType=ability.getCounterType();
	}

	@Override
	protected void executeAbility() throws CastException {
		creature = (Creature) getAbilityTarget();
		if (creature != null) {
			//counters = Behaviour.getBehavior(getBehaviors());
			//for (Behaviour b : counters) {
				CardCountCounter cardCountCounter = new CardCountCounter();
				cardCountCounter.setType(counterType);
				cardCountCounter.setLocation(counterLocation);
				switch (counterTarget) {
				case SELF:
					cardCountCounter.getPlayers().add(getPlayer());
					break;
				case OPPONENT:
					cardCountCounter.getPlayers().add(getPlayer().getOpponent());
					break;
				case ANY:
					cardCountCounter.getPlayers().add(getPlayer());
					cardCountCounter.getPlayers().add(getPlayer().getOpponent());
					break;
				}
				creature.addBehaviour(cardCountCounter);
			//}
			registerUndo();
		}
	}


	public Target getCounterTarget() {
		return counterTarget;
	}

	public Type getCounterType() {
		return counterType;
	}

	public Location getCounterLocation() {
		return counterLocation;
	}

	@Override
	public AddCardCountCounter clone() {
		return new AddCardCountCounter(this);
	}
	
	@Override
	public boolean isInputRequired(){
		return true;
	}

	public void setCounterTarget(Target counterTarget) {
		this.counterTarget = counterTarget;
	}

	public void setCounterType(Type counterType) {
		this.counterType = counterType;
	}

	public void setCounterLocation(Location counterLocation) {
		this.counterLocation = counterLocation;
	}
	
	
	
	
	

}
